clc; clear; close all;

% =============== 1. 画 T-GM 的 4 张子图 ===============

% 1.1 (a) POP for T-GM
subplot(2,4,1);
hold on; grid on; box on;
X3_range = linspace(-5, 5, 2001);  % T-GM 取 X3 ∈ [-5, 5]
F3 = TGM_POP(X3_range);  % 计算 phi3(n+1)-phi3(n)
plot(X3_range, F3, 'r', 'LineWidth',1.2);
% x 轴刻度适当调整或添加标记
xlabel('phi_3(n)'); ylabel('\phi_3(n+1) - \phi_3(n)');
title('(a) POP for T-GM');


% 1.2 计算 T-GM 在 DC 条件下的 V3, I3 (b)(c)(d) 子图需要
[X3_DC, V3_DC, I3_DC] = TGM_DC_curve(-5, 5, 2001);

% 1.2.1 (b) DC X3 - I3
subplot(2,4,2);
hold on; grid on; box on;
plot(X3_DC, I3_DC, 'b', 'LineWidth',1.2);
xlabel('X_3'); ylabel('I_3');
title('(b) DC X_3 - I_3 curves of T-GM');

% 1.2.2 (c) DC X3 - V3
subplot(2,4,3);
hold on; grid on; box on;
plot(X3_DC, V3_DC, 'm', 'LineWidth',1.2);
xlabel('X_3'); ylabel('V_3');
title('(c) DC X_3 - V_3 curves of T-GM');

% 1.2.3 (d) DC V3 - I3
subplot(2,4,4);
hold on; grid on; box on;
plot(V3_DC, I3_DC, 'k', 'LineWidth',1.2);
xlabel('V_3'); ylabel('I_3');
title('(d) DC V_3 - I_3 curves of T-GM');

% =============== 2. 画 C-GM 的 4 张子图 ===============

% 2.1 (e) POP for C-GM
subplot(2,4,5);
hold on; grid on; box on;
X4_range = linspace(-10, 10, 3001);  % C-GM 取 X4 ∈ [-10, 10]
F4 = CGM_POP(X4_range);  % 计算 phi4(n+1)-phi4(n)
plot(X4_range, F4, 'r', 'LineWidth',1.2);
xlabel('X_4'); ylabel('\phi_4(n+1) - \phi_4(n)');
title('(e) POP for C-GM');

%%%%%%%%%%%
axesPos = get(gca,'Position');  % [left bottom width height], 0~1归一化

insetWidth = 0.36 * axesPos(3);   % 小图宽度占主图宽度的30%
insetHeight = 0.18 * axesPos(4);  % 小图高度占主图高度的40%
insetPos = [ ...
axesPos(1) + 0.59 * axesPos(3), ...  % 小图左侧略偏右
axesPos(2) + 0.05 * axesPos(4), ...  % 小图底部略偏上
insetWidth, ...
insetHeight ...
];

% 3. 创建小图(新的axes)
ax_inset = axes('Position', insetPos);


% 4. 在小图中重新绘制相同曲线
plot(X4_range, F4, 'r', 'LineWidth',1.2);
hold on; grid on; box on;

% 5. 设定局部放大区间
xlim([-0.5, 0.5]); ylim([-0.01, 0.01]);

%%%%%%%%%%%%

% 2.2 计算 C-GM 在 DC 条件下的 V4, I4 (f)(g)(h) 子图需要
[X4_DC, V4_DC, I4_DC] = CGM_DC_curve(-10, 10, 3001);

% 2.2.1 (f) DC X4 - I4
subplot(2,4,6);
hold on; grid on; box on;
plot(X4_DC, I4_DC, 'b', 'LineWidth',1.2);
xlabel('X_4'); ylabel('I_4');
title('(f) DC X_4 - I_4 curves of C-GM');

% 2.2.2 (g) DC X4 - V4
subplot(2,4,7);
hold on; grid on; box on;
plot(X4_DC, V4_DC, 'm', 'LineWidth',1.2);
xlabel('X_4'); ylabel('V_4');
title('(g) DC X_4 - V_4 curves of C-GM');

% 2.2.3 (h) DC V4 - I4
subplot(2,4,8);
hold on; grid on; box on;
plot(V4_DC, I4_DC, 'k', 'LineWidth',1.2);
xlabel('V_4'); ylabel('I_4');
title('(h) DC V_4 - I_4 curves of C-GM');

% 总标题
sgtitle('Nonvolatile and Local Active Characteristics of the T-GM and C-GM');


%% =============== T-GM 部分 ===============

% (1) T-GM 的 POP: v3(n)=0 时,  phi3(n+1)-phi3(n) 随 X3 的变化
function F = TGM_POP(X3)
    % 由论文可得:
    % phi3(n+1) = 0.86*phi3(n) + 0.12*phi3(n)*|phi3(n)| - 0.02*phi3(n)^3
    % => phi3(n+1)-phi3(n) = -0.14*X3 + 0.12*X3*|X3| - 0.02*X3^3
    F = -0.14.*X3 + 0.12.*X3.*abs(X3) - 0.02.*(X3.^3);
end

% (2) T-GM 的 DC 分析: phi3(n+1)=phi3(n) => 求出 V3(X3) 和 I3(X3)
function [X3, V3, I3] = TGM_DC_curve(xmin, xmax, N)
    % X3 范围
    X3 = linspace(xmin, xmax, N);

    % 根据论文推导:
    % DC 条件: phi3(n+1)=phi3(n)
    % => 0.86*X3 + 0.12*X3*|X3| - 0.02*X3^3 + 0.1*V3 - X3 = 0
    % => 0.1*V3 = X3 - 0.86*X3 - 0.12*X3*|X3| + 0.02*X3^3
    % => V3 = 10*( X3*(1-0.86) - 0.12*X3*|X3| + 0.02*X3^3 )
    % => V3 = 10*( 0.14*X3 - 0.12*X3*|X3| + 0.02*X3^3 )
    % 论文中简化后写成:
    % V3(X3) = 1.4X3 - 1.2X3|X3| + 0.2X3^3
    V3 = 1.4.*X3 - 1.2.*X3.*abs(X3) + 0.2.*(X3.^3);

    % I3 = sin(X3)*V3(X3)
    I3 = sin(X3) .* V3;
end

%% =============== C-GM 部分 ===============

% (1) C-GM 的 POP: v4(n)=0 时, phi4(n+1)-phi4(n) 随 X4 的变化
function F = CGM_POP(X4)
    % phi4(n+1) = 0.1*tanh(phi4(n)^3) + 0.99*phi4(n)
    % => phi4(n+1)-phi4(n) = 0.1*tanh(X4^3) - 0.01*X4
    F = 0.1.*tanh(X4.^3) - 0.01.*X4;
end

% (2) C-GM 的 DC 分析: phi4(n+1)=phi4(n) => 求出 V4(X4) 和 I4(X4)
function [X4, V4, I4] = CGM_DC_curve(xmin, xmax, N)
    % X4 范围
    X4 = linspace(xmin, xmax, N);

    % DC 条件: phi4(n+1)=phi4(n)
    % => 0.1*tanh(X4^3) + 0.99*X4 + 0.1*V4 - X4 = 0
    % => 0.1*V4 = X4 - 0.99*X4 - 0.1*tanh(X4^3)
    % => 0.1*V4 = 0.01*X4 - 0.1*tanh(X4^3)
    % => V4 = 0.1*( X4 ) - 1*( tanh(X4^3) )  (乘10)
    % 论文中写成: V4(X4)= -0.1*tanh(X4^3) + 0.01*X4
    % (此处要严格对应原文的系数)
    V4 = -0.1.*tanh(X4.^3) + 0.01.*X4;

    % I4 = cos(X4)*V4(X4)
    I4 = cos(X4) .* V4;
end
